package com.jojo.bubble.controller.api;

import com.alipay.api.AlipayApiException;
import com.jojo.bubble.common.domain.AjaxResult;
import com.jojo.bubble.entity.*;
import com.jojo.bubble.service.*;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;

import static com.jojo.bubble.utils.AlipayTradeWapPay.authLogin;

/**
 * @Author JoJo
 * @Date 2023/7/22 7:28
 * @Description
 * @Version 1.0
 */
@RestController
@RequestMapping("/api")
public class IndexApiController {
    @Resource
    UserService userService;
    @Resource
    GearService gearService;

    @Resource
    PriceService priceService;

    @Resource
    DevService devService;
    @Resource
    AgentService agentService;

    /**
     * @desc: 静默登录
     * @param: [code, pid]
     * @return: com.jojo.ocr_car.common.domain.AjaxResult
     * @date: 2022/11/29 17:08
     */
    @RequestMapping("/authLogin")
    public AjaxResult toWeChatLogin(String code) {
        return userService.toWeChatLogin(code);
    }

    @RequestMapping("/authAli")
    public AjaxResult toAliChatLogin(String code) throws AlipayApiException {

        return userService.toAliLogin(code);
    }

    @GetMapping("/gear/list")
    public AjaxResult getGear(GearVo gearVo) {
        Dev dev = devService.getInfo(gearVo.getDevId());
        if (!ObjectUtils.isEmpty(dev)) {
            Agent agent = agentService.getInfo(dev.getAgentId());
            if (!ObjectUtils.isEmpty(agent)) {
                Price price = new Price();
                price.setAgentId(agent.getId());
                List<Price> prices = priceService.getList(price);
                if (!ObjectUtils.isEmpty(prices)) {
                    return AjaxResult.success(prices);
                }
            }
        }
        Gear gear = new Gear();
        gear.setStatus(gearVo.getStatus());
        List<Gear> gearList = gearService.getList(gear);
        return AjaxResult.success(gearList);
    }

    @GetMapping("/price/list")
    public AjaxResult getPrice(GearVo gearVo) {

        Agent agent = agentService.getInfo(gearVo.getAgentId());

        Price price = new Price();
        price.setAgentId(agent.getId());
        price.setStatus(gearVo.getStatus());
        List<Price> prices = priceService.getList(price);
        if (ObjectUtils.isEmpty(prices)) {
            List<Gear> gearList = gearService.getList(new Gear());
            for (Gear gear : gearList) {
                Price price1 = new Price();
                price1.setPrice(gear.getPrice());
                price1.setRunTime(gear.getRunTime());
                price1.setAgentId(gearVo.getAgentId());
                priceService.insert(price1);
                prices.add(price1);
            }

        }
        return AjaxResult.success(prices);


    }
    @GetMapping("/price/info")
    public AjaxResult priceInfo(Price price) {
        Price target = priceService.getInfo(price.getId());
        return AjaxResult.success(target);
    }
    @PostMapping("/price/update")
    public AjaxResult priceUpdate(Price price) {

        priceService.update(price);

        return AjaxResult.success();
    }

    @GetMapping("/dev/list")
    public AjaxResult devList() {
        Dev dev = new Dev();
        dev.setStatus(1);
        List<Dev> devList = devService.getList(dev);
        return AjaxResult.success(devList);
    }

    @GetMapping("/dev/info")
    public AjaxResult devInfo(int id) {
        Dev dev = devService.getInfo(id);
        return AjaxResult.success(dev);
    }
}
